package com.zzzs.volatiledemo;

import java.util.concurrent.CountDownLatch;

/**
 * 重排序测试
 * @author zhu_shuai
 * @since 9/2
 */
public class Demo3 {

    private static int a=0, b=0, x=0, y=0;

    public static void main(String[] args) throws InterruptedException {
        int i=1;
        for (; ; ) {
            a=0;
            b=0;
            x=0;
            y=0;
            CountDownLatch countDownLatch = new CountDownLatch(2);
            new Thread(()-> {
                a=1;
                x=b;
                countDownLatch.countDown();
            }).start();
            new Thread(()-> {
                b=1;
                y=a;
                countDownLatch.countDown();
            }).start();
            countDownLatch.await();
            if (x == 0 && y == 0) {
                System.out.printf("第%d次%n", i);
                break;
            }
            i++;
        }
    }

}
